package cn.csc.module.order.controller.admin.returnsapply;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.order.controller.admin.returnsapply.vo.*;
import cn.csc.module.order.convert.returnsapply.OrderReturnsApplyConvert;
import cn.csc.module.order.dal.dataobject.returnsapply.OrderReturnsApplyDO;
import cn.csc.module.order.service.returnsapply.OrderReturnsApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 售后申请")
@RestController
@RequestMapping("/order/returns-apply")
@Validated
public class OrderReturnsApplyController {

    @Resource
    private OrderReturnsApplyService returnsApplyService;

    @PostMapping("/create")
    @ApiOperation("创建售后申请")
    @PreAuthorize("@ss.hasPermission('order:returns-apply:create')")
    public CommonResult<BigInteger> createReturnsApply(@Valid @RequestBody OrderReturnsApplyCreateReqVO createReqVO) {
        return success(returnsApplyService.createReturnsApply(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新售后申请")
    @PreAuthorize("@ss.hasPermission('order:returns-apply:update')")
    public CommonResult<Boolean> updateReturnsApply(@Valid @RequestBody OrderReturnsApplyUpdateReqVO updateReqVO) {
        returnsApplyService.updateReturnsApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除售后申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:returns-apply:delete')")
    public CommonResult<Boolean> deleteReturnsApply(@RequestParam("id") BigInteger id) {
        returnsApplyService.deleteReturnsApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得售后申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:returns-apply:query')")
    public CommonResult<OrderReturnsApplyRespVO> getReturnsApply(@RequestParam("id") BigInteger id) {
        OrderReturnsApplyDO returnsApply = returnsApplyService.getReturnsApply(id);
        return success(OrderReturnsApplyConvert.INSTANCE.convert(returnsApply));
    }

    @GetMapping("/list")
    @ApiOperation("获得售后申请列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('order:returns-apply:query')")
    public CommonResult<List<OrderReturnsApplyRespVO>> getReturnsApplyList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<OrderReturnsApplyDO> list = returnsApplyService.getReturnsApplyList(ids);
        return success(OrderReturnsApplyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得售后申请分页")
    @PreAuthorize("@ss.hasPermission('order:returns-apply:query')")
    public CommonResult<PageResult<OrderReturnsApplyRespVO>> getReturnsApplyPage(@Valid OrderReturnsApplyPageReqVO pageVO) {
        PageResult<OrderReturnsApplyDO> pageResult = returnsApplyService.getReturnsApplyPage(pageVO);
        return success(OrderReturnsApplyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出售后申请 Excel")
    @PreAuthorize("@ss.hasPermission('order:returns-apply:export')")
    @OperateLog(type = EXPORT)
    public void exportReturnsApplyExcel(@Valid OrderReturnsApplyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderReturnsApplyDO> list = returnsApplyService.getReturnsApplyList(exportReqVO);
        // 导出 Excel
        List<OrderReturnsApplyExcelVO> datas = OrderReturnsApplyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "售后申请.xls", "数据", OrderReturnsApplyExcelVO.class, datas);
    }

}
